package threadcoreknowledge.stopthreads;

/**
 * 最佳实践：catchInterruptedException 之后优先选择：
 * 在方法签名中抛出异常
 */
public class RightWayStopThreadInProd implements Runnable {
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(new RightWayStopThreadInProd());
        t.start();
        Thread.sleep(1000);
        t.interrupt();
    }

    @Override
    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            System.out.println("go");
            throwInMethod();
        }
    }

    private void throwInMethod() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
